package com.api.controller.auth;

import com.alibaba.fastjson.JSON;
import com.api.annotation.AuthToken;
import com.api.controller.BaseController;
import com.api.service.BaseService;
import com.api.service.auth.RoleService;
import com.api.service.log.LogUserService;
import com.api.util.AjaxResult;
import com.api.util.LogUtil;
import com.api.util.MapUtil;
import com.api.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @packageName: com.api.controller.auth
 * @name: RoleController
 * @description:
 * @author: 董定卓
 * @dateTime: 2019/6/18 11:46
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    RoleService service;

    @Autowired
    LogUserService logUserService;

    @Override
    protected BaseService getService() {
        return service;
    }

    /**
     * 添加角色
     *
     * @param map
     * @return
     */
    @PostMapping("/add")
    @AuthToken
    public AjaxResult add(@RequestParam Map<String, Object> map) {
        if (MapUtil.getString(map, "name") == null) {
            return AjaxResult.failResult("角色名称不能为空");
        }
        map.put("state", 1);
        int i = service.add(map);
        if (i > 0) {
            Map<String, Object> logUserMap = LogUtil.addUserLog(UserUtil.getUserDetail().getId(), "/role/add", JSON.toJSONString(map));
            logUserService.add(logUserMap);
            return AjaxResult.successResult();
        }
        return AjaxResult.failResult("添加失败");
    }

    /**
     * 删除角色
     *
     * @param map
     * @return
     */
    @PostMapping("/del")
    @AuthToken
    public AjaxResult del(@RequestParam Map<String, Object> map) {
        if (MapUtil.getString(map, "id") == null) {
            return AjaxResult.failResult("角色ID不能为空");
        }
        map.put("state", -1);
        int i = service.update(map);
        if (i > 0) {
            Map<String, Object> logUserMap = LogUtil.addUserLog(UserUtil.getUserDetail().getId(), "/role/del", JSON.toJSONString(map));
            logUserService.add(logUserMap);
            return AjaxResult.successResult();
        }
        return AjaxResult.failResult("删除失败");
    }

    /**
     * 修改角色
     *
     * @param map
     * @return
     */
    @PostMapping("/update")
    @AuthToken
    public AjaxResult update(@RequestParam Map<String, Object> map) {
        if (MapUtil.getString(map, "id") == null) {
            return AjaxResult.failResult("角色ID不能为空");
        }
        if (MapUtil.getString(map, "name") == null && MapUtil.getString(map, "state") == null) {
            return AjaxResult.failResult("角色修改的内容不能为空");
        }
        int i = service.update(map);
        if (i > 0) {
            Map<String, Object> logUserMap = LogUtil.addUserLog(UserUtil.getUserDetail().getId(), "/role/update", JSON.toJSONString(map));
            logUserService.add(logUserMap);
            return AjaxResult.successResult();
        }
        return AjaxResult.failResult("修改失败");
    }


    /**
     * 角色分页
     *
     * @param map
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/page")
    @Override
    @AuthToken
    public AjaxResult page(@RequestParam Map<String, Object> map, @RequestParam(value = "limit", defaultValue = "10") int limit, @RequestParam(value = "page", defaultValue = "1") int page) {
        Map<String, Object> logUserMap = LogUtil.addUserLog(UserUtil.getUserDetail().getId(), "/role/page", JSON.toJSONString(map));
        logUserService.add(logUserMap);
        return super.page(map, limit, page);
    }
}
